using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Skywave.Windows.Forms
{
    [System.Serializable()]
    public enum Shapes
    {
        Rectangle,
        Oval
    }
    public partial class Shape : UserControl, IPrintOn
    {
        public Shape()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(Shape_Paint);
            this.SizeChanged += new EventHandler(Shape_SizeChanged);
        }

        private Shapes mShape = Shapes.Rectangle;
        public Shapes ShapeType
        {
            get { return mShape; }
            set { mShape = value; this.Invalidate(); }
        }

        private bool mFilled = false;
        public bool Filled
        {
            get { return mFilled; }
            set { mFilled = value; this.Invalidate(); }
        }

        private int mLineWidth = 2;
        public int LineWidth
        {
            get { return mLineWidth; }
            set { mLineWidth = value; this.Invalidate(); }
        }

        void Shape_SizeChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        System.Drawing.Region paintRegion;
        void Shape_Paint(object sender, PaintEventArgs e)
        {
            PrintOn(e.Graphics, this.DisplayRectangle);
            this.Region = paintRegion;
        }

        #region IPrintOn Members

        public void PrintOn(Graphics g, Rectangle bounds)
        {
            Rectangle rect = bounds;
            System.Drawing.Drawing2D.GraphicsPath xPath1 = new System.Drawing.Drawing2D.GraphicsPath();
            System.Drawing.Drawing2D.GraphicsPath xPath2 = new System.Drawing.Drawing2D.GraphicsPath();
            System.Drawing.Region excludeRegion;
            Brush foreBrush, backBrush;
            Rectangle innerRect;

            foreBrush = new SolidBrush(this.ForeColor);
            backBrush = new SolidBrush(this.BackColor);
            switch (mShape)
            {
                case Shapes.Rectangle:
                    xPath1.AddRectangle(rect);
                    paintRegion = new Region(xPath1);
                    g.FillRectangle(foreBrush, rect);
                    if (!mFilled)
                    {
                        innerRect = new Rectangle(rect.X + mLineWidth, rect.Y + mLineWidth, rect.Width - 2 * mLineWidth, rect.Height - 2 * mLineWidth);
                        g.FillRectangle(backBrush, innerRect);
                        xPath2.AddRectangle(innerRect);
                        excludeRegion = new Region(xPath2);
                        paintRegion.Exclude(excludeRegion);
                    }
                    break;
                case Shapes.Oval:
                    xPath1.AddEllipse(rect);
                    paintRegion = new Region(xPath1);
                    g.FillEllipse(foreBrush, rect);
                    if (!mFilled)
                    {
                        innerRect = new Rectangle(rect.X + mLineWidth, rect.Y + mLineWidth, rect.Width - 2 * mLineWidth, rect.Height - 2 * mLineWidth);
                        g.FillEllipse(backBrush, innerRect);
                        xPath2.AddEllipse(innerRect);
                        excludeRegion = new Region(xPath2);
                        paintRegion.Exclude(excludeRegion);
                    }
                    break;
                default:
                    break;
            }
        }

        #endregion
    }
}
